package com.liveneo.adapter.base.service.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.service.LogService;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.system.base.utils.ReflectionUtil;

/**
 * 日志处理服务实现类
 * 
 * @author fucy
 *
 */
@Service
public class LogServiceImpl implements LogService {
    /**
     * 根据Map保存错误日志
     */
    @Override
    public void saveErrorLog(Map<String, String> logInfo, Exception e) {
        saveErrorLog(logInfo.get(AdapterConstants.LOG_KEY_LOG_TYPE), logInfo.get(AdapterConstants.LOG_KEY_CASE_NO),
                logInfo.get(AdapterConstants.LOG_KEY_CASE_UUID), logInfo.get(AdapterConstants.LOG_KEY_LOSS_UUID),
                logInfo.get(AdapterConstants.LOG_KEY_LOG_TYPE), e);
    }

    /**
     * 使用参数保存错误日志
     */
    @Override
    public void saveErrorLog(String logType, String userCode, String caseNo, String caseUuid, String lossUuid, Exception e) {
        // 将异常转换成字符串
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        saveLog(logType, userCode, caseNo, caseUuid, lossUuid, sw.toString());
    }

    /**
     * 保存日志
     */
    @Override
    public void saveLog(String logType, String userCode, String caseNo, String caseUuid, String lossUuid, String logMsg) {
        String logMethod = ReflectionUtil.getFirstControllerMethodName();
        // 获取调用的方法
        System.err.println(logType + ":" + userCode + ":" + caseNo + ":" + caseUuid + ":" + lossUuid + ":" + logMethod + ":" + logMsg);
    }

    @Override
    public int addLogTodb(String logUuid, String userCode, String caseNo, String logType, String logMethod, String logMsg) {
        // TODO Auto-generated method stub
        return 0;
    }
}
